import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/login/uslogin.vue'
import Unlogin from '@/views/unlogin/index.vue'
import PersonalCenter from '@/views/PersonalCenter/index.vue'
import Myhomebody from '@/views/PersonalCenter/Header/myHomeBody.vue'
import syxelement from '@/views/PersonalCenter/element/syxelement/index.vue'
import attention from '@/views/PersonalCenter/element/attention/index.vue'
import recommend from '@/views/recommend/index.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/',
    name: 'PersonalCenter',
    component: PersonalCenter,
    children: [
      {
        path: '',
        name: 'Myhomebody',
        component: Myhomebody,
        children: [
          {
            path: '/syxelement',
            name: 'syxelement',
            component: syxelement
          },
          {
            path: '/attention',
            name: 'attention',
            component: attention
          }
        ]
      },
      {
        path: '/recommend',
        name: 'recommend',
        component: recommend
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/unlogin',
    name: 'unlogin',
    component: Unlogin
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// router.beforeEach((to, from, next) => {
//   if (to.path !== '/login') {
//     JSON.parse(window.localStorage.getItem('user'))
//       ? next()
//       : next('/login')
//   } else {
//     next()
//     if (to.path === '/login' || to.path === '/unlogin') {
//       next()
//     } else {
//       JSON.parse(window.localStorage.getItem('user'))
//         ? next()
//         : next('/login')
//     }
//   }
// })

export default router
